package com.xzq.bugstack.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @Author xzq
 * @Description //TODO
 * @Date 2021/11/20 18:26
 * @Version 1.0.0
 **/
public class NettyServer {

   private NioEventLoopGroup boss = new NioEventLoopGroup();
   private NioEventLoopGroup worker = new NioEventLoopGroup();
    private Channel channel;

    public void lister(int port) {
        ChannelFuture f = null;
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new MyChannelInitializer());
             f = b.bind(port).sync();
           channel= f.channel();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (null != f && f.isSuccess()) {
                System.out.println("Netty文件服务器启动成功.....");
            }else{
                System.out.println("Netty文件服务器启动失败.....");
            }
        }
    }
}
